<template>
  <view style="padding-top: 1px;background-color: white">
    <u-tabbar :active="useStore.activeTab"
              :border="false"
              :fixed="true"
              badgeStyle="transition: all 0.5s ease;"
              :activeColor="'#4ea4f9'"
              :placeholder="true"
              :safeAreaInsetBottom="true">
      <u-tabbar-item v-for="(item, index) in tabBarList"
                     :key="index" :icon="getTabbarIcon(item, index)"
                     :badge="index === 3 ? useStore.unreadMessageNumber : 0"
                     :text="item.text"
                     @click="handleTabbarItemClick(item, index)"></u-tabbar-item>
    </u-tabbar>
  </view>
</template>

<script setup>
import useUserStore from '@/store/user.js'
import {getToken} from "@/utils/auth";
let useStore = useUserStore()
  const  tabBarList =  [
    {
      text: "首页",
      pagePath: "/pages/index/index",
      iconPath: "/static/tabs/index/index.png",
      selectedIconPath: "/static/tabs/index/index_active.png"
    },
    {
      text: "话题",
      pagePath: "/pages/topic/topic",
      iconPath: "/static/tabs/topic/topic.png",
      selectedIconPath: "/static/tabs/topic/topic_active.png"
    },
    {
      pagePath: "/pages/post/post",
      iconPath: "/static/tabs/bg-add.png",
      selectedIconPath: "/static/tabs/bg-add.png",
      text: ""
    },
    {
      text: "消息",
      pagePath: "/pages/msg/msg",
      iconPath: "/static/tabs/msg/msg.png",
      selectedIconPath: "/static/tabs/msg/msg_active.png"
    },
    {
      text: "我的",
      pagePath: "/pages/my/my",
      iconPath: "/static/tabs/my/my.png",
      selectedIconPath: "/static/tabs/my/my_active.png"
    }
  ]
//点击tabbar按钮
  const handleTabbarItemClick = (item, index) => {
    if (useStore.activeTab !== index) {
      //如果点击的是扫描按钮
      if (index === 5) {
       //根据详细需求书写代码
      } else if(index === 2){
        let token = getToken();
        if(token.length > 0){
          uni.navigateTo({
            url: '/pages/post/PostIndex', // 修改为你要跳转的页面路径
            animationType: 'slide-in-bottom'
          });
        }else {
          uni.navigateTo({
            url: '/pages/my/login', // 修改为你要跳转的页面路径
          });
        }
      }else{
        useStore.setActive(index)
        const path = item.pagePath
        uni.switchTab({
          url: path
        })
      }
    }
  }
//图标的切换
  const getTabbarIcon = (item, index) => {
    return useStore.activeTab === index ? item.selectedIconPath : item.iconPath
  }
</script>

